#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=10000000, mods=998244353, maxp=10000000;

void initBC(int *b, int *c, int n, int bx, int by, int cx, int cy, int p) {
  b[0]=by+1;c[0]=cy+1;
  for(int i=1;i<n;i++){
    b[i]=((long long)b[i-1]*bx+by)%p+1;
    c[i]=((long long)c[i-1]*cx+cy)%p+1;
  }
}

long long min(int a, int b) {
  return a<b ? a : b;
}

long long qpower(long long a, int n)  {
  long long s=1;
  for (; n>1; n/=2) {
    if (n%2) {
      s = a*s%mods;
    }
    a = a*a%mods;
  }
  return a*s%mods;
}

int main() {
  freopen("forging.in", "r", stdin);
  freopen("forging.out", "w", stdout);

  static long long f[maxn+1], inv[maxp+1];
  inv[1] = 1;
  for (int i(2),k;i<maxp;++i) {
    k=mods/i,inv[i]=1ll*inv[mods-k*i]*(mods-k)%mods;
  }
  static int b[maxn+1], c[maxn+1];
  int n, a, bx, by, cx, cy, p;
  scanf("%d %d %d %d %d %d %d", &n, &a, &bx, &by, &cx, &cy, &p);
  initBC(b, c, n, bx, by, cx, cy, p);

  f[0] = a;
  if (n>=1) {
    long long p=inv[min(c[0], b[0])]*c[0]%mods;
    f[1] = (f[0]*p%mods+f[0])%mods;
  }
  for (int i=2; i<=n; i++) {
    long long p=inv[min(c[i-1], b[i-2])]*c[i-1]%mods;
    f[i] = (f[i-1]*p%mods+f[i-2])%mods;
  }
  printf("%lld", f[n]);

  fclose(stdout);
  fclose(stdin);
  return 0;
}
